define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('@mod/fieldEdit').base;

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				urlRemove: 'ptc/remove'
				,urlRecover: 'ptc/recover'
				,urlHide: 'ptc/hide'
			});
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = self.getConfig();

			this.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('图文创意模板表')+'</span>',
					'<div class="mb10">',
						'<span class="" pub-name="importFile" pub-mod="@base/common/input.button"/>',
					'</div>',
					'<div class="mb10">',
						'<span class="" pub-name="add" pub-mod="@base/common/input.button"/>',
					'</div>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="ptc/grid/template.base"/>'
			].join('')));


			this.buildFromTemplate({
				importFile: {
					value: '导入模板信息'
					,'class': 'btn btn-primary'
					,width: null
				}
				,add: {
					value: '单个分类添加'
					,'class': 'btn btn-success'
					,width: null
				}
				,grid: {
					'hasSearch': true,
					'hasFilter': true
				}
			});

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
					{'name': 'adxId', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('渠道：'),
						'url': 'ptc/list',
						'param': {
							Type: 1
							,mongoField: ['name', 'AdxId']
						},
						'all': true,
						'key': 'AdxId',
						'name': 'name'
					}},
					{'name': 'IsDeleted', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('模板隐藏：'),
						'data': [
							{id: 0, name: LANG('正常')},
							{id: 1, name: LANG('隐藏')}
						],
						'value': 0
					}}
				]
			});

		}
		,onInputClick: function(ev) {
			var url = '';
			switch (ev.name) {
				case 'importFile':
					url = 'ptc/template/import';
					break;
				case 'add':
					url = 'ptc/template/edit';
					break;
				default :
					return false;
			}

			pubjs.controller.navigate(url);
		}
		,onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'adxId':
						param.AdxId = id;
						break;
					case 'channel':
						param.ChannelId = +id;
						break;
					case 'IsDeleted':
						param.IsDeleted = !!id;
						break;
				}
			});

			self.$.grid.setParam(param).load();
		}
		,reload: function() {
			this.$.grid.load();
		}
		,reset: function() {

		}
		,onListOpClick:function(ev){
			//编辑单条记录
			var self = this;
			var c = self.getConfig();
			var id = ev.param.data._id;
			switch (ev.param.op) {
				case 'edit':
					pubjs.controller.navigate('ptc/template/edit/' + id);
					break;
				case 'del':
					pubjs.confirm(LANG('确认要删除此记录吗, 此删除是不可恢复的, 如有子类也会随之删除'), function(res) {
						if(res) {
							pubjs.loading.show();
							pubjs.mc.send(c.urlRemove, {_id: id}, function(err) {
								pubjs.loading.hide();
								if(err) {
									pubjs.alert(err.message);
									return false;
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
					break;
				case 'recover':
				case 'hide':
					var isHide = ev.param.op == 'hide';
					var url =  isHide ? c.urlHide : c.urlRecover;
					pubjs.loading.show();
					pubjs.mc.send(url, {_id: id}, function(err) {
						pubjs.loading.hide();
						if(err) {
							pubjs.alert(err.message);
							return false;
						}
						var tip = isHide ? LANG('隐藏成功') : LANG('恢复成功');
						pubjs.alert(tip);
						self.reload();
					});
					break;
			}

			return false;
		}
	});

	exports.list = List;



	exports.edit = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{text: LANG('所属渠道'), tip: LANG('模板所属渠道'), type: 'dropdown', field: 'ParentId', validate: true,
						config: {
							url: 'ptc/list'
							,param: {Type: 1}
							,'reqType': 'websocket'
							,'key': 'AdxId'
							,'name': 'name'
							,"def": LANG("请选择")
						}
					}
					,{text: LANG('名称'), tip: LANG('模板的名称'), type: 'input', field: 'name', validate: true}
					,{text: LANG('Title'), tip: LANG('模板的title'), type: 'input', field: 'title'}
					,{text: LANG('内部Id'), tip: LANG('内部Id'), type: 'input', field: 'BdxId', validate: false}
					,{text: LANG('模板Id'), tip: LANG('模板Id'), type: 'input', field: 'templateId', validate: true}
					,{text: LANG('终端类型'), tip: LANG('模块属于的类型,以逗号分隔, 1 > IOS， 2 > Android, 100 > 其它'), type: 'input', field: 'terminal_type',
/*						config: {
							options: [
								{_id: 0, Name: LANG('全部')}
								,{_id: 1, Name: LANG('iOS')}
								,{_id: 2, Name: LANG('Android')}
								,{_id: 100, Name: LANG('其它')}
							]
							,"search": false
							,"def": LANG("请选择")
							,width: 150
						}*/
					}
					,{text: LANG('CreativeSpec'), tip: LANG('没填就取原来的template_id末3位作为模版'), type: 'input', field: 'CreativeSpec'}
					,{text: LANG('顺序'), tip: LANG('字段的显示顺序，默认是0，从大到小排列,越大的值越在前面显示'), type: 'input', field: 'index'}
				],
				Title: LANG('模板信息:'),
				urlUpdate: 'ptc/update',
				urlAdd: 'ptc/add',
				urlInfo: 'ptc/info'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,setData: function() {
			this.Super('setData', arguments);
			this.disable('ParentId');
			this.disable('templateId');
			this.disable('BdxId');
			return this;
		}
		,getData: function() {
			var data = this.Super('getData', arguments);
			if(data.terminal_type)
			{
				data.terminal_type = data.terminal_type.split(',');
			}

			return data;
		}
	});

	exports.import = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{'text': '渠道', tip: LANG('标识导入的模板所属渠道'), type: 'dropdown', field: 'AdxId', validate: true, 'config': {
						'title': LANG('渠道'),
						'url': 'ptc/list',
						'param': {
							Type: 1
							,mongoField: ['name', 'AdxId']
						},
						'reqType': 'websocket',
						'key': 'AdxId',
						'def': '请选择渠道',
						'name': 'name'
					}},
					{text: LANG('模板的配置'), tip: LANG('以数组的格式把数据贴上来'), type: 'textarea', field: 'config', rows: 30, validate: true}
				],
				Title: LANG('模板导入:'),
				urlAdd: 'ptc/importTemplate'
			});

			this.Super('init', arguments);
		}
	});
});